<template>
  <div v-loading="loading" class="news-page">
    <div class="search-box">
      <span>文章标题</span>
      <el-input
        v-model="searchInput.title"
        placeholder="请输入内容"
        clearable
        size="medium"
        style="width: 200px"
      />
      <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
    </div>
    <div class="add-button">
      <el-button type="success" round size="mini" @click="handleAdd">
        新增+
      </el-button>
    </div>
    <!-- 新闻列表管理 -->
    <el-table
      v-loading="loading"
      :data="newsList"
      border
      element-loading-text="Loading"
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 + (Pagination.pageNumber - 1) * Pagination.pageSize }}
        </template>
      </el-table-column>
      <el-table-column label="文章标题" width="220" height="60">
        <template slot-scope="scope">
          {{ scope.row.title | ellipsis }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="文章封面" width="200" height="60">
        <template slot-scope="scope">
          <img :src="scope.row.cover" :alt="scope.row.cover" style="width: 100px; height: 60px;">
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新时间">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          {{ scope.row.updatedAt }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            round
            @click="handleEdit(scope.row._id)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            round
            @click="handleDelete(scope.row._id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        background
        :total="Pagination.total"
        :current-page.sync="Pagination.pageNumber"
        layout="total, prev, pager, next"
        :page-size="Pagination.pageSize"
      />
    </div>
  </div>
</template>

<script>
import { getNewList, deleteNews } from '@/api/news'
export default {
  data() {
    return {
      newsList: [],
      loading: false,
      searchInput: {
        title: ''
      },
      Pagination: {
        pageNumber: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  created() {
    this.getListItems()
  },
  methods: {
    async getListItems(input = {}) {
      const { pageNumber, pageSize } = this.Pagination
      try {
        this.loading = true
        const res = await getNewList({ pageNumber, pageSize, ...input })
        this.newsList = res.data
        this.Pagination = res.pagination
        this.loading = false
      } catch (error) {
        console.log(error)
      }
    },
    handleAdd() {
      this.$router.push('/news/add')
    },
    handleSearch() {
      this.loading = true
      if (this.searchInput.title.trim() === '') {
        this.$message.error('请输入搜索内容')
        this.loading = false
        return false
      }
      this.loading = true
      this.getListItems(this.searchInput)
      this.loading = false
    },
    handleEdit(id) {
      this.$router.push(`/news/edit?id=${id}`)
    },
    // 删除新闻
    handleDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.loading = true
        await deleteNews(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loading = false
        // 刷新列表
        this.getListItems()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.news-page {
  margin: 0 20px;
}
.add-button {
  display: flex;
  padding: 20px 0;
  justify-content: flex-end;
}
.search-box {
  margin: 20px 0 0 50px;
  span {
    margin-right: 10px;
  }
  .el-button {
    margin-left: 10px;
  }
}
.pagination {
  display: flex;
  justify-content: center;
  padding: 20px 0;
}
</style>
